package org.joshsacks.fishies.ui.gl;

import static javax.microedition.khronos.opengles.GL10.GL_CLAMP_TO_EDGE;
import static javax.microedition.khronos.opengles.GL10.GL_DEPTH_TEST;
import static javax.microedition.khronos.opengles.GL10.GL_LINEAR;
import static javax.microedition.khronos.opengles.GL10.GL_NEAREST;
import static javax.microedition.khronos.opengles.GL10.GL_REPLACE;
import static javax.microedition.khronos.opengles.GL10.GL_SMOOTH;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_2D;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_ENV;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_ENV_MODE;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_MAG_FILTER;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_MIN_FILTER;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_WRAP_S;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_WRAP_T;

import java.io.OutputStreamWriter;

import javax.microedition.khronos.opengles.GL10;

import org.joshsacks.fishies.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLDebugHelper;
import android.opengl.GLUtils;

public class TextureManager {
  private static int[] ICON_RESOURCES = {
//    R.drawable.diver_64, R.drawable.fish_64, R.drawable.marine_star_64, 
//    R.drawable.octopus_64, R.drawable.submarine_64, R.drawable.fish_bowl_64,
    R.drawable.bluefish_128, R.drawable.clown_128, R.drawable.shark_128,
    R.drawable.starfih_128, R.drawable.turtle_128, R.drawable.pidgin_128,
  };
  private int[] textures;
  int initCount = 0;
  
  public void init(Context context, GL10 gl) {  
    setGlParams(gl);
    textures = new int[ICON_RESOURCES.length]; 
    gl.glGenTextures(ICON_RESOURCES.length, textures, 0); 
    for (int i = 0; i < ICON_RESOURCES.length; i++) { 
      bindTexture(gl, context, textures[i], ICON_RESOURCES[i]);
    }
  }

  public static GL10 debugWrapGl(GL10 gl) {
    return gl;
//    return (GL10) GLDebugHelper.wrap(gl, GLDebugHelper.CONFIG_CHECK_GL_ERROR | 
//            GLDebugHelper.CONFIG_CHECK_THREAD | GLDebugHelper.CONFIG_LOG_ARGUMENT_NAMES, 
//            new OutputStreamWriter(System.err));
  }
  
  public static void bindTexture(GL10 gl, Context context, int textureId, int resourceId) { 
    gl.glBindTexture(GL_TEXTURE_2D, textureId);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;
    options.inDither = false;
    options.inDensity = 0;
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resourceId, options); 
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    GLUtils.texImage2D(GL_TEXTURE_2D, 0, bmp, 0);
    bmp.recycle();
  }

  private void setGlParams(GL10 gl) {    
    gl.glActiveTexture(GL10.GL_TEXTURE0);
    gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
            GL_CLAMP_TO_EDGE);
    gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
            GL_CLAMP_TO_EDGE);

    gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL10.GL_DECAL);
    gl.glShadeModel(GL_SMOOTH);
    gl.glDisable(GL_DEPTH_TEST);
    gl.glEnable(GL_TEXTURE_2D);
   }

  public void setTexture(GL10 gl, int i) {
    gl.glColor4f(0.0f, 0.0f, 0.0f, 1.0f);   
    gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL10.GL_DECAL);  
    gl.glBindTexture(GL_TEXTURE_2D, textures[i]);
  }  
}
